package com.hertz.alarm.controller.api;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hertz.alarm.controller.BaseController;
import com.hertz.alarm.controller.support.ResultModel;
import com.hertz.alarm.entity.MatchNba;
import com.hertz.alarm.service.MatchNbaService;
import com.hertz.alarm.util.DateUtils;
@Controller
@RequestMapping("/api/alarmmanager")
public class ApiAlarmManagerController  extends BaseController{
	
	@Resource
	private MatchNbaService matchNbaService;
	
	@RequestMapping(value="nextAlarmTime",method=RequestMethod.POST)
	public @ResponseBody ResultModel<Object> nextAlarmTime(
			@RequestParam(value="current",required=false) Long current){
		if(current == null || current == 0)
			current = System.currentTimeMillis();
		String ymd = DateUtils.timeFormat(current, DateUtils.Y_M_D);
		List<MatchNba> list = matchNbaService.listByYmd(ymd);
		
		if(list == null || list.size() == 0){
			return buildResultModel(DateUtils.nextDay());
		}
		Collections.sort(list, new Comparator<MatchNba>() {

			@Override
			public int compare(MatchNba o1, MatchNba o2) {
				return o1.getMatchTime().compareTo(o2.getMatchTime());
			}
		});
		for(MatchNba item : list){
			long matchTime = DateUtils.string2Time(item.getMatchDay() + " " + item.getMatchTime(), DateUtils.YMD_HM);
			if(current < matchTime){
				return buildResultModel(matchTime);
			}
		}
		
		return buildResultModel(DateUtils.nextDay());
	}
	
	
}
